/* -----------------------------------------------------
 iCompose
 OptionView.m
 
 Created by Team: THE BEARD on 31/10/09
 Members who contributed to this file:
 1. Ethan Holmes
 
 List of changes to previous member:
 Version 1:
 Oct 31, 2009 (Ethan)
 - implemented all methods
 
 Simon Fraser University, 2009.
 ----------------------------------------------------- */

#import "OptionsView.h"

@implementation OptionsView

@synthesize nameField;
@synthesize timeControl;
@synthesize tempoLabel;

/*
 Changes the value of the Tempo according to the Tempo Slider.
 */
-(IBAction)tempoSliderChange:(id)sender {
	UISlider *slider = (UISlider*)sender;
	int tempoValue = (int)(slider.value);
	NSString *text = [[NSString alloc] initWithFormat:@"%i", tempoValue];
	tempoLabel.text = text;
	NSLog(@"Tempo value slider changed to: %@", text);
	[text release];
}

-(IBAction)dismissKeyBoard {
	NSLog(@"The keyboard has been dismissed.");
	[nameField resignFirstResponder];
}

-(IBAction)back {
	[self.parentViewController dismissModalViewControllerAnimated:YES];
}

/*
 Allocates a Key Signature based on the symbol and Accidental value,
 Time signature desegnated by the noteField and beatField, 
 Tempo desegnated by the Tempo Label,
 and Instrument player.
 
 If the user fails to input a Composition name then an alert is displayed.
 
 The mainview's staff is then initialized with the corresponding Objects, and
 presented as the new view to the user.
 */
-(IBAction)switchNew {
	MainView *mainView = [[MainView alloc] initWithNibName:@"MainView" bundle:nil];
	NSLog(@"Initializing a new MainView with settings: ");
	
	TimeSignature *timeSig = [TimeSignature alloc];
	[timeSig initTime: 4 andValue: 4];
	NSLog(@"Time signature with: 4 / 4 (only option availiable in version 1)");
	
	Tempo *tempo = [Tempo alloc];
	[tempo setSpeed: [tempoLabel.text intValue]];
	NSLog(@"Tempo with speed: %@", tempoLabel.text);
	
	InstrumentPlayer *instrumentPlayer = [[InstrumentPlayer alloc] init];
	NSString *name = nameField.text;
	NSLog(name);
	if([name isEqualToString: @""] || !([name rangeOfString:@" "].location == NSNotFound)){
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"" message: @"Please enter a proper compostion name." delegate: nil cancelButtonTitle: @"Ok" otherButtonTitles:nil];
		NSLog(@"Alert has been issued.");
		[alert show];
		[alert release];
		return;
	}
	
	[mainView initStaffState:timeSig andTempo: tempo andInstrument: instrumentPlayer andName: name];
	NSLog(@"Initializing new MainView's staff with settings.");
	NSLog(@"Initialization successful.");
	
	[self presentModalViewController:mainView animated: YES];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	int tempoValue = 30;
	NSString *text = [[NSString alloc] initWithFormat:@"%i", tempoValue];
	tempoLabel.text = text;
	[text release];
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

@end
